package com.zretc.controller;


import com.zretc.entity.House;
import com.zretc.entity.HouseManager;
import com.zretc.entity.Track;
import com.zretc.service.HouseManagerService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/houseMangerController")
public class HouseMangerController {
    private static final Logger logger = LoggerFactory.getLogger(HouseMangerController.class);
    @Autowired
    private HouseManagerService houseManagerService;

    /**
     * 添加仓库
     * @param house
     * @return
     */
    @RequestMapping("/insertHouse")
    @ResponseBody
    public  int insertHouse(House house){
        return houseManagerService.houseManagerAddHouse(house);
    }
    /**
     * 查询地址编号
     * @return
     */
   @RequestMapping("/findAddressId")
   @ResponseBody
   public List<String> findAddressId(){
    List<String> list = houseManagerService.getAddressIdList();
    return list;
   }

    /**
     * 删除仓库
     * @param houseId
     * @return
     */
    @RequestMapping("/deleteByHouseId")
    @ResponseBody
   public  int deleteByHouseId(String houseId){
       return houseManagerService.deleteByHouseId(houseId);
   }

    /**
     * 修改仓库信息
     * @param houseId
     * @param houseName
     * @param houseLimit
     * @return
     */
   @RequestMapping("/updateHose")
   @ResponseBody
   public int updateHose(
           @RequestParam("houseId") String houseId ,
           @RequestParam("houseName") String houseName,
           @RequestParam("houseLimit") Double houseLimit){
       return houseManagerService.updateHoseInfo(houseId,houseName,houseLimit);
   }

    /**
     * 查询所有的仓库信息
     * @return
     */
   @RequestMapping("/findAllHouse")
   @ResponseBody
   public List<House> findAllHouse(){
        List<House>  list = houseManagerService.getAllHouseList();
        return list;
   }

    /**
     * 通过仓库编号查询仓库信息
     * @param houseId
     * @return
     */
   @RequestMapping("/findByhouseId")
   @ResponseBody
   public House findByhouseId(String houseId){
        House house = houseManagerService.getByHouseList(houseId);
        return house;
   }

    /**
     * 根据操作码查询出入库信息
     * @param opCode
     * @return
     */
    @RequestMapping("/findEntryInfo")
    @ResponseBody
    public List<Track> findEntryInfo(String opCode){
        List<Track> list = houseManagerService.getEntityAddDeilveryList(opCode);
       return list;
    }

    @RequestMapping("/findHouseManagerById")
    @ResponseBody
    public HouseManager findHouseManagerById(HttpServletRequest httpServletRequest){

        // 从session中获取用户编号
        HttpSession session = httpServletRequest.getSession();
        String houseManagerId = (String) session.getAttribute("houseManagerId");

        HouseManager houseManager = houseManagerService.findHouseManagerById("001");

        return houseManager;
    }

    @RequestMapping("/updateHouseManagerById")
    @ResponseBody
    public HouseManager updateHouseManagerById(String houseManagerName, String houseManagerPsw, MultipartFile houseManagerFace, HttpServletRequest httpServletRequest) throws IOException {
        String fileSuffix = "";
        String date = "";

        if(houseManagerFace.getSize() != 0){
            // 获取文件名
            String filename = houseManagerFace.getOriginalFilename();

            // 自定义文件名 + 文件后缀名
            fileSuffix = filename.substring(filename.lastIndexOf("."));

            date = System.currentTimeMillis() + "";

            // 文件存储在指定的服务器中
            File file = new File("D:/EDS/images/user/" + date + fileSuffix);

            houseManagerFace.transferTo(file);
        }

        // 从session中获取用户编号
        HttpSession session = httpServletRequest.getSession();
        String houseManagerId = (String) session.getAttribute("houseManagerId");

        Map<String,Object> map = new HashMap<>();
        map.put("houseManagerId","001");
        map.put("houseManagerName",houseManagerName);
        map.put("houseManagerPsw",houseManagerPsw);
        map.put("houseManagerFace",date + fileSuffix);

        houseManagerService.updateHouseManagerById(map);

        HouseManager houseManager = houseManagerService.findHouseManagerById("001");

        return houseManager;
    }
}
